package com.yaqian.wolf.controller;

import com.yaqian.wolf.comment.JwtUtil;
import com.yaqian.wolf.comment.Result;
import com.yaqian.wolf.entity.User;
import com.yaqian.wolf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rent")
public class UserHandler {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    HttpServletRequest request;
    @GetMapping("/userpage/{page}/{size}")
    public Page<User> pages(@PathVariable("page")Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return userRepository.findAll(pageable);
    }
//    @GetMapping("/getHeaders")
//    public Result listAllHeaders(@RequestHeader Map<String, String> headers) {
//        headers.forEach((key, value) -> {
//            // 日志中输出所有请求头
//            System.out.println(String.format("Header '%s' = %s", key, value));
//        });
//        System.out.println(JwtUtil.validateToken( request.getHeader("authorization").substring(7)).getData());
//        Result ok =new Result();
//        ok.setMessage("pok");
//        return ok ;
//    }
        @PostMapping("/addUser")
        public Result addUser(@RequestBody User user){
            Result res =new Result();
        try {
             User ok = user;
            System.out.println(userRepository.save(ok));
            res.success();
             return res;
         }catch (Exception e)
         {
             res.addError();
             return res;
         }
            }
        @GetMapping("/findUser/{key}")
        public Result Find(@PathVariable String key){
        Result res =new Result();
        try {
            System.out.println(key);
            User user = userRepository.findById(key).get();
            System.out.println(user);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("user", user);
            res.setData(map);
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError();
            return res;
        }
    }


    @GetMapping("/deleteUser/{key}")
    public Result delete(@PathVariable String key){
        Result res =new Result();
        try {
            System.out.println(key);
             userRepository.deleteById(key);
             
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError("没有这个用户名或者其他原因");
            return res;
        }
    }
    @GetMapping("/User")
    public void ji(){
        System.out.println(userRepository.findAllByTelephone("1735989631"));

    }


}
